<%@page import="java.util.Calendar"%>
<%@page import="edu.cmu.aes.project2.PlaceOrder"%>
<%@page import="org.bpel.sample.bpel.sample.PlaceorderRequestOrder"%>
<%@page import="edu.cmu.aes.project2.ws.data.UserInformationDto"%>
<%@page import="edu.cmu.aes.project2.ws.data.UserInformationServiceProxy"%>
<%@page import="edu.cmu.aes.project2.ws.data.UserInformationService"%>
<%@page import="edu.cmu.aes.project2.SupplierService"%>
<%
	request.setAttribute("title", "Purchase page");
	String username = UISecurityUtil.getUserName(request);
	if(username == null){
		response.sendRedirect("unauthorizedAccess.jsp");
		return;
	}

	String errorMessage = "";
	boolean isError = false;
	boolean isPurchase = false;
	String brandName = request.getParameter("brandName");
	if(brandName == null)
		brandName = "";
	String modelName = request.getParameter("modelName");
	if(modelName == null)
		modelName = "";
	String priceStr = request.getParameter("price");
	if(priceStr == null)
		priceStr = "";
	
	int price = 0;
	try{
		price = (int)Double.parseDouble(priceStr);
	}catch(Exception e){
	}
	if(price == 0){
		errorMessage = "Order cannot process because there is no price information";
		isError = true;
	}
		
	//String creditCardNo = "";
	//String cvCode = "";
	String creditCardNo = request.getParameter("creditCardNo");
	if(creditCardNo == null)
		creditCardNo = "";
	String cvCode = request.getParameter("cvCode");
	if(cvCode == null)
		cvCode = "";
	boolean isSubmitted = request.getParameter("purchase") != null;
	if(isSubmitted && !isError){
		UserInformationService userInfoService = new UserInformationServiceProxy();
		UserInformationDto userInfoDto = userInfoService.getUserInformation(username);
		if(userInfoDto == null){
			errorMessage = "User information is not found. Please login again or contact web master";
		}else{
			PlaceOrder placeorderService = new PlaceOrder();
			PlaceorderRequestOrder order = new PlaceorderRequestOrder();
			order.setBrand(brandName);
			order.setModel(modelName);
			order.setQuantity(1);
			String customerName = userInfoDto.getLastName()+", "+ userInfoDto.getFirstName();
			errorMessage = placeorderService.process(customerName, price, Calendar.getInstance(), order);
			if(!"Success!".equals(errorMessage)){
				isError = true;
			}
		}
	}
%>
<%@include file='/WEB-INF/template/header.jsp'%>
<%@include file='/WEB-INF/template/right-menu.jsp'%>
<script type="text/javascript">
	function regIsDigit(fData)
	{
	    //var reg = new RegExp("^[0-9]$");
	    //return (reg.match(fData));
	    return fData.match(/^\d+$/) != null;
	}

	function testpurchase(){
		var creditCardNo = $("#creditCardStr").val();
		var cvCode = $("#cvCodeStr").val();

		if(creditCardNo.length != 16){
			alert("credit card number must have 16 digits");
			return false;
		}

		if(!regIsDigit(creditCardNo)){
			alert("credit card number must be digit");
			return false;
		}

		if(cvCode.length != 3){
			alert("CV code must have 3 digits");
			return false;
		}
		if(!regIsDigit(cvCode)){
			alert("CV code must be digit");
			return false;
		}

		$("#creditCardNo").val(creditCardNo);
		$("#cvCode").val(cvCode);
		$("#purchase").attr('disabled', 'disabled');
		//$("#purchase_form").submit();
		return true;
	}
</script>

		
<div id="content">
Hello Biker!!! <br/>
<br/>
<% if(isError){ %>
	<font color="red"><%=errorMessage %></font><br/><br/>
<% }%>
	
	<% if(isSubmitted & !isError) { %>
	Your order has been placed successfully.
	<% }else{ %>
	<form name="purchase_form" id="purchase_form" onSubmit="return testpurchase()" action="purchase.jsp" method="post" >
	<input type="hidden" name="brandName" id="brandName" value="<%=brandName %>">
	<input type="hidden" name="modelName" id="modelName" value="<%=modelName%>">
	<input type="hidden" name="price" id="price" value="<%=priceStr%>">
	<input type="hidden" name="creditCardNo" id="creditCardNo" value="<%=creditCardNo%>">
	<input type="hidden" name="cvCode" id="cvCode" value="<%=cvCode%>">
	
	Please input your credit card number and CV code <br/>
	Credit card no : <input type = "text" id = "creditCardStr" value = "<%=creditCardNo %>" maxlength="16"> <br/>
	CV code : <input type = "text" id = "cvCodeStr" value = "<%=cvCode %>" maxlength="3"> <br/>
	<input type="submit" name="purchase" value="purchase" />
	</form>
	<% } %>

</div>
<%@include file='/WEB-INF/template/footer.jsp'%>
